<?php

namespace app\controller;

use think\Controller;
use think\Session;

class Index extends Controller
{

    //登录验证
    public function _initialize()
    {
        if ( !session( 'isLogin' ) ) {
            $this->redirect( 'login/index' );
        }
    }

    public function index()
    {
        return view();
    }

    public function imageup()
    {
        // 获取表单上传文件、
        $file = request()->file( 'image' );
        // 移动到框架应用根目录/public/static/uploads/ 目录下
        $info = $file->validate( [ 'ext' => 'jpg,png,gif' ] )->move( ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads' );
        if ( $info ) {

            $return = [ 'status' => 'true', 'info' => 'http://' . config( 'imageUrl' ) . '/uploads/' . $info->getSaveName() ];
        } else {
            // 上传失败获取错误信息
            $return = [ 'status' => 'false', 'info' => $file->getError() ];

        }
        return '<script>parent.imageup(' . json_encode( $return ) . ')</script>';
    }
}
